<template>
  <el-dialog :title="title"  :visible="visible" :close-on-click-modal="false"
             :before-close="handleClose" width="500px">
    <el-form :model="formData" :rules="rules" ref="formData" label-width="80px">
      <el-form-item label="课程类型" prop="courseType" >
        <el-input v-model="formData.courseType"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="text" @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="handleSubmit"
                 :loading="loading">{{loading?'提交中...':'确认提交'}}</el-button>
    </span>
  </el-dialog>
</template>

<script>

  import {update} from "@/api/modules/courseType";
  
  export default {
    name: "edit",
    props: {
      visible: {
        type: Boolean
      },
      title: {
        type: String,
        default: '新增'
      },

      defaultFormData: {
        type: Object
      },
    },
    data() {
      return {

        loading: false,
        
        formData: {
          water: '',
          electric: ''
        },
        rules: {
          courseType: [
            {required: true, message: "密码不能为空", trigger: "blur"}
          ]
        },
        
        refresh: false
      }
    },
    mounted() {

      //this.getAllListRole()
      //console.log(JSON.stringify(this.defaultFormData) == "{}")
      if (JSON.stringify(this.defaultFormData) == "{}") {

      }else{
        let formData = this.clone(this.defaultFormData)
        this.formData = JSON.parse(JSON.stringify(formData))
      }
    },
    methods: {
      handleClose() {
        this.$emit('close', this.refresh)
      },
      handleSubmit() {
        this.$refs.formData.validate(valid => {
          if (valid) {
            this.loading = true
            
              update({
                ...this.formData,
              }).then(res => {
                this.loading = false
                if (res.success) {
                  this.refresh = true
                  this.toast(res.message)
                  this.handleClose()
                }
              }).catch(data =>{
                    this.toast(data,5)
                    this.refresh = true
                    this.handleClose()
                }) 
           
          }
        })
      }
    }
  }
</script>

<style scoped>

</style>
